﻿using System;
using System.Data;
using System.Collections.Generic;
using Maticsoft.Common;
using Hermes.Model;
using System.Linq;

namespace Hermes.BLL
{
    /// <summary>
    /// Orders
    /// </summary>
    public partial class Orders
    {
        private readonly Hermes.DAL.Orders dal = new Hermes.DAL.Orders();
        public Orders()
        { }
        #region  Method

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public long Add(Hermes.Model.Orders model)
        {
            long id = 0;
            try
            {

                foreach (Hermes.Model.OrderLines line in model.OrderLines)
                {
                    (new Hermes.BLL.OrderLines()).Add(line);
                }
                id = dal.Add(model);
            }
            catch (Exception ex)
            {
                (new Hermes.BLL.OrderLines()).Delete(model.OrderNumber);
                dal.Delete(model.OrderNumber, model.SiteID);
                throw ex;
            }



            return id;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Hermes.Model.Orders model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(long ID)
        {

            return dal.Delete(ID);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            return dal.DeleteList(IDlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Hermes.Model.Orders GetModel(long ID)
        {

            return dal.GetModel(ID);
        }

        public Hermes.Model.Orders GetModel(string ordernumber, int siteID)
        {
            return dal.GetModel(ordernumber, siteID);
        }

        public Hermes.Model.Orders GetModel(string trackingNo)
        {
            return dal.GetModel(trackingNo);
        }

        public Hermes.Model.WebService.UnCarrierOrdersInfo[] GetUnCarrierOrders(int siteID)
        {
            DataSet ds = dal.GetUnCarrierOrders(siteID);

            List<Hermes.Model.WebService.UnCarrierOrdersInfo> countryAndOrderCount = new List<Hermes.Model.WebService.UnCarrierOrdersInfo>();

            if (ds != null && ds.Tables != null && ds.Tables.Count > 0 && ds.Tables[0].Rows != null)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; ++i)
                {
                    Hermes.Model.WebService.UnCarrierOrdersInfo info = new Model.WebService.UnCarrierOrdersInfo();
                    if (ds.Tables[0].Rows[0]["Country"] != null && ds.Tables[0].Rows[0]["Country"].ToString() != "")
                    {
                        info.Country = ds.Tables[0].Rows[0]["Country"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["count"] != null && ds.Tables[0].Rows[0]["count"].ToString() != "")
                    {
                        info.Count = Convert.ToInt32(ds.Tables[0].Rows[0]["count"]);
                    }
                    countryAndOrderCount.Add(info);
                }
                return countryAndOrderCount.ToArray();
            }
            else
            {
                return null;
            }


        }

        public Hermes.Model.WebService.FulfillmentInfo[] GetFulfilInfos(int siteID, int storeID, int carrierID, DateTime startDate, DateTime endDate)
        {
            DataSet ds = dal.GetFulfilInfos(siteID,storeID,carrierID,startDate,endDate);
            List<Hermes.Model.WebService.FulfillmentInfo> fulfillInfos = new List<Hermes.Model.WebService.FulfillmentInfo>();

            if (ds != null && ds.Tables != null && ds.Tables.Count > 0 && ds.Tables[0].Rows != null)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; ++i)
                {
                    Hermes.Model.WebService.FulfillmentInfo info = new Hermes.Model.WebService.FulfillmentInfo();
                    if (ds.Tables[0].Rows[0]["Ordernumber"] != null && ds.Tables[0].Rows[0]["Ordernumber"].ToString() != "")
                    {
                        info.Ordernumber = ds.Tables[0].Rows[0]["Ordernumber"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["trackCode"] != null && ds.Tables[0].Rows[0]["trackCode"].ToString() != "")
                    {
                        info.TrackingNo = ds.Tables[0].Rows[0]["trackCode"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["shippingon"] != null && ds.Tables[0].Rows[0]["shippingon"].ToString() != "")
                    {
                        info.ShippingDate = Convert.ToDateTime(ds.Tables[0].Rows[0]["shippingon"]);
                    }
                    fulfillInfos.Add(info);
                }
                return fulfillInfos.ToArray();
            }
            else
            {
                return null;
            }
        }

        public Hermes.Model.WebService.UnSyncOrderInfo[] UnSyncOrders(int carrierID)
        {
            DataSet ds = dal.UnSyncOrders(carrierID);

            List<Hermes.Model.WebService.UnSyncOrderInfo> unSyncOrders = new List<Hermes.Model.WebService.UnSyncOrderInfo>();

            if (ds != null && ds.Tables != null && ds.Tables.Count > 0 && ds.Tables[0].Rows != null)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; ++i)
                {
                    Hermes.Model.WebService.UnSyncOrderInfo info = new Model.WebService.UnSyncOrderInfo();
                    if (ds.Tables[0].Rows[i]["OrderNumber"] != null && ds.Tables[0].Rows[i]["OrderNumber"].ToString() != "")
                    {
                        info.Ordernumber = ds.Tables[0].Rows[i]["OrderNumber"].ToString();
                    }
                    if (ds.Tables[0].Rows[i]["SiteID"] != null && ds.Tables[0].Rows[i]["SiteID"].ToString() != "")
                    {
                        info.SiteID = Convert.ToInt32(ds.Tables[0].Rows[i]["SiteID"]);
                    }
                    if (ds.Tables[0].Rows[i]["ID"] != null && ds.Tables[0].Rows[i]["ID"].ToString() != "")
                    {
                        info.OrderID = Convert.ToInt32(ds.Tables[0].Rows[i]["ID"]);
                    }
                    unSyncOrders.Add(info);
                }
                return unSyncOrders.ToArray();
            }
            else
            {
                return null;
            }
        }

        public Hermes.Model.WebService.UnLabelOrderInfo[] UnLabelOrders(int carrierID)
        {
            DataSet ds = dal.UnLabelOrders(carrierID);

            List<Hermes.Model.WebService.UnLabelOrderInfo> unLabelOrders = new List<Hermes.Model.WebService.UnLabelOrderInfo>();

            if (ds != null && ds.Tables != null && ds.Tables.Count > 0 && ds.Tables[0].Rows != null)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; ++i)
                {
                    Hermes.Model.WebService.UnLabelOrderInfo info = new Model.WebService.UnLabelOrderInfo();
                    if (ds.Tables[0].Rows[i]["OrderNumber"] != null && ds.Tables[0].Rows[i]["OrderNumber"].ToString() != "")
                    {
                        info.Ordernumber = ds.Tables[0].Rows[i]["OrderNumber"].ToString();
                    }
                    if (ds.Tables[0].Rows[i]["SiteID"] != null && ds.Tables[0].Rows[i]["SiteID"].ToString() != "")
                    {
                        info.SiteID = Convert.ToInt32(ds.Tables[0].Rows[i]["SiteID"]);
                    }
                    if (ds.Tables[0].Rows[i]["ID"] != null && ds.Tables[0].Rows[i]["ID"].ToString() != "")
                    {
                        info.OrderID = Convert.ToInt32(ds.Tables[0].Rows[i]["ID"]);
                    }
                    if (ds.Tables[0].Rows[i]["TrackCode"] != null && ds.Tables[0].Rows[i]["TrackCode"].ToString() != "")
                    {
                        info.TrackingNo = ds.Tables[0].Rows[i]["TrackCode"].ToString();
                    }
                    unLabelOrders.Add(info);
                }
                return unLabelOrders.ToArray();
            }
            else
            {
                return null;
            }
        }

        public int AssignCarrier(int carrierID, int siteID, string[] countries)
        {
            return dal.AssignCarrier(carrierID, siteID, countries);
        }

        public int GetOrdersLabel(int[] orderid)
        {
            return dal.UpdateOrdersStatus(orderid, OrderStatus.GetLabel);
        }

        public int ShipOrder(long orderID, string shippingUser)
        {
            return dal.ShipOrder(orderID, shippingUser);
        }


        public int UpdateTrackingNo(string orderNumber, int siteID, long orderID, string trackingNo)
        {
            return dal.UpdateTrackingNo(orderNumber, siteID, orderID, trackingNo);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中
        /// </summary>
        public Hermes.Model.Orders GetModelByCache(long ID)
        {

            string CacheKey = "OrdersModel-" + ID;
            object objModel = Maticsoft.Common.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(ID);
                    if (objModel != null)
                    {
                        int ModelCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
                        Maticsoft.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (Hermes.Model.Orders)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Hermes.Model.Orders> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Hermes.Model.Orders> DataTableToList(DataTable dt)
        {
            List<Hermes.Model.Orders> modelList = new List<Hermes.Model.Orders>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Hermes.Model.Orders model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Hermes.Model.Orders();
                    if (dt.Rows[n]["ID"] != null && dt.Rows[n]["ID"].ToString() != "")
                    {
                        model.ID = long.Parse(dt.Rows[n]["ID"].ToString());
                    }
                    if (dt.Rows[n]["OrderNumber"] != null && dt.Rows[n]["OrderNumber"].ToString() != "")
                    {
                        model.OrderNumber = dt.Rows[n]["OrderNumber"].ToString();
                    }
                    if (dt.Rows[n]["Status"] != null && dt.Rows[n]["Status"].ToString() != "")
                    {
                        model.Status = int.Parse(dt.Rows[n]["Status"].ToString());
                    }
                    if (dt.Rows[n]["OrderTime"] != null && dt.Rows[n]["OrderTime"].ToString() != "")
                    {
                        model.OrderTime = DateTime.Parse(dt.Rows[n]["OrderTime"].ToString());
                    }
                    if (dt.Rows[n]["Amount"] != null && dt.Rows[n]["Amount"].ToString() != "")
                    {
                        model.Amount = decimal.Parse(dt.Rows[n]["Amount"].ToString());
                    }
                    if (dt.Rows[n]["SiteID"] != null && dt.Rows[n]["SiteID"].ToString() != "")
                    {
                        model.SiteID = int.Parse(dt.Rows[n]["SiteID"].ToString());
                    }
                    if (dt.Rows[n]["AddTime"] != null && dt.Rows[n]["AddTime"].ToString() != "")
                    {
                        model.AddTime = DateTime.Parse(dt.Rows[n]["AddTime"].ToString());
                    }
                    if (dt.Rows[n]["Contact"] != null && dt.Rows[n]["Contact"].ToString() != "")
                    {
                        model.Contact = dt.Rows[n]["Contact"].ToString();
                    }
                    if (dt.Rows[n]["Company"] != null && dt.Rows[n]["Company"].ToString() != "")
                    {
                        model.Company = dt.Rows[n]["Company"].ToString();
                    }
                    if (dt.Rows[n]["Street"] != null && dt.Rows[n]["Street"].ToString() != "")
                    {
                        model.Street = dt.Rows[n]["Street"].ToString();
                    }
                    if (dt.Rows[n]["City"] != null && dt.Rows[n]["City"].ToString() != "")
                    {
                        model.City = dt.Rows[n]["City"].ToString();
                    }
                    if (dt.Rows[n]["Province"] != null && dt.Rows[n]["Province"].ToString() != "")
                    {
                        model.Province = dt.Rows[n]["Province"].ToString();
                    }
                    if (dt.Rows[n]["Country"] != null && dt.Rows[n]["Country"].ToString() != "")
                    {
                        model.Country = dt.Rows[n]["Country"].ToString();
                    }
                    if (dt.Rows[n]["CountryCode"] != null && dt.Rows[n]["CountryCode"].ToString() != "")
                    {
                        model.CountryCode = dt.Rows[n]["CountryCode"].ToString();
                    }
                    if (dt.Rows[n]["Postcode"] != null && dt.Rows[n]["Postcode"].ToString() != "")
                    {
                        model.Postcode = dt.Rows[n]["Postcode"].ToString();
                    }
                    if (dt.Rows[n]["Phone"] != null && dt.Rows[n]["Phone"].ToString() != "")
                    {
                        model.Phone = dt.Rows[n]["Phone"].ToString();
                    }
                    if (dt.Rows[n]["Email"] != null && dt.Rows[n]["Email"].ToString() != "")
                    {
                        model.Email = dt.Rows[n]["Email"].ToString();
                    }
                    if (dt.Rows[n]["TrackCode"] != null && dt.Rows[n]["TrackCode"].ToString() != "")
                    {
                        model.TrackCode = dt.Rows[n]["TrackCode"].ToString();
                    }
                    if (dt.Rows[n]["ModifyOn"] != null && dt.Rows[n]["ModifyOn"].ToString() != "")
                    {
                        model.ModifyOn = DateTime.Parse(dt.Rows[n]["ModifyOn"].ToString());
                    }
                    if (dt.Rows[n]["ShippingMethod"] != null && dt.Rows[n]["ShippingMethod"].ToString() != "")
                    {
                        model.ShippingMethod = int.Parse(dt.Rows[n]["ShippingMethod"].ToString());
                    }
                    if (dt.Rows[n]["StoreID"] != null && dt.Rows[n]["StoreID"].ToString() != "")
                    {
                        model.StoreID = int.Parse(dt.Rows[n]["StoreID"].ToString());
                    }
                    if (dt.Rows[n]["ShippingOn"] != null && dt.Rows[n]["ShippingOn"].ToString() != "")
                    {
                        model.ShippingOn = DateTime.Parse(dt.Rows[n]["ShippingOn"].ToString());
                    }
                    if (dt.Rows[n]["ShippingBy"] != null && dt.Rows[n]["ShippingBy"].ToString() != "")
                    {
                        model.ShippingBy = dt.Rows[n]["ShippingBy"].ToString();
                    }
                    if (dt.Rows[n]["ImportOn"] != null && dt.Rows[n]["ImportOn"].ToString() != "")
                    {
                        model.ImportOn = DateTime.Parse(dt.Rows[n]["ImportOn"].ToString());
                    }
                    if (dt.Rows[n]["ImportBy"] != null && dt.Rows[n]["ImportBy"].ToString() != "")
                    {
                        model.ImportBy = dt.Rows[n]["ImportBy"].ToString();
                    }
                    if (dt.Rows[n]["LastUpdate"] != null && dt.Rows[n]["LastUpdate"].ToString() != "")
                    {
                        model.LastUpdate = DateTime.Parse(dt.Rows[n]["LastUpdate"].ToString());
                    }
                    if (dt.Rows[n]["CustomerID"] != null && dt.Rows[n]["CustomerID"].ToString() != "")
                    {
                        model.CustomerID = int.Parse(dt.Rows[n]["CustomerID"].ToString());
                    }
                    if (dt.Rows[n]["PickUpAddressID"] != null && dt.Rows[n]["PickUpAddressID"].ToString() != "")
                    {
                        model.PickUpAddressID = int.Parse(dt.Rows[n]["PickUpAddressID"].ToString());
                    }
                    if (dt.Rows[n]["ShipFromAddressID"] != null && dt.Rows[n]["ShipFromAddressID"].ToString() != "")
                    {
                        model.ShipFromAddressID = int.Parse(dt.Rows[n]["ShipFromAddressID"].ToString());
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}


        public bool SameOrder(Hermes.Model.Orders order, Hermes.Model.Orders otherOrder)
        {
            bool same = true;
            //订单头是否相等
            if (order.OrderNumber != otherOrder.OrderNumber ||
                order.SiteID != otherOrder.SiteID ||
                order.StoreID != otherOrder.StoreID ||
                order.Street != otherOrder.Street ||
                order.Amount != otherOrder.Amount ||
                order.City != otherOrder.City ||
                order.Company != otherOrder.Company ||
                order.Contact != otherOrder.Contact ||
                order.Country != otherOrder.Country ||
                order.CountryCode != otherOrder.CountryCode ||
                order.CustomerID != otherOrder.CustomerID ||
                //order.Email != otherOrder.Email ||
                order.Phone != otherOrder.Phone ||
                order.Postcode != otherOrder.Postcode ||
                order.Province != otherOrder.Province
                )
            {
                same = false;
            }
            if (order.OrderLines.Count != otherOrder.OrderLines.Count)
            {
                same = false;
            }

            foreach (Hermes.Model.OrderLines line in order.OrderLines)
            {
                //otherOrder.OrderLines.Find(
                var temp = from l in otherOrder.OrderLines
                           where
                               l.OrderNUmber == line.OrderNUmber
                               && l.SKU == line.SKU && l.QTY == line.QTY
                               && l.Price == line.Price
                           select l;
                if (temp == null)
                {
                    same = false;
                }
            }

            if (order.StoreID != otherOrder.StoreID)
            {
                same = false;
            }
            return same;

        }




        #endregion  Method
    }
}

